﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using System.Collections;
using ExpertMIS.Entities;
using ExpertMIS.UserControl;
using System.Reflection;

namespace ExpertMIS.Columns
{
    public partial class ViewBulletin : System.Web.UI.Page
    {
        private BulletinCatalog _catalog = BulletinCatalog.SocailBulletin;

        private void LoadBulletin(string bulletinOID)
        {
            BulletinBroker broker = new BulletinBroker();
            Hashtable htKeys = new Hashtable();
            htKeys.Add("OID", bulletinOID);
            BulletinEntity bulletin = broker.GetEntity(htKeys) as BulletinEntity;
            if (bulletin != null)
            {
                lblBulletin.Text = bulletin.Bulletin;
                pContext.InnerHtml = bulletin.Context;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadBulletin(Request["oid"]);
        }

        protected override void OnPreInit(EventArgs e)
        {
            // TODO, 切换不同的master

            _catalog = (BulletinCatalog)Enum.Parse(typeof(BulletinCatalog), Request["catalog"]);

            switch (_catalog)
            {
                case BulletinCatalog.OrganizationBulletin:
                    {
                        MasterPageFile = "~/Columns/Bulletin.master";
                    }
                    break;
                case BulletinCatalog.SocailBulletin:
                    {
                        MasterPageFile = "~/Forums/Visitor.Master";
                    }
                    break;
            }

            base.OnPreInit(e);

            EventInfo eventInfo = Master.GetType().GetEvent("OnLoadLeftPanels");
            if (eventInfo != null && eventInfo.EventHandlerType == typeof(EventHandler))
            {
                eventInfo.AddEventHandler(Master, new EventHandler(ViewBulletin_OnLoadLeftPanels));
            }
        }

        private void ViewBulletin_OnLoadLeftPanels(object sender, EventArgs e)
        {
            ucBulletinList ucBulletins = LoadControl("~/UserControl/ucBulletinList.ascx") as ucBulletinList;
            if (ucBulletins != null)
            {
                ucBulletins.ShowTitle = true;
                ucBulletins.LengthVariable = "ViewBulletin";
                ucBulletins.BulletinCatalog = _catalog;
                ucBulletins.ShowNewIcon = false;
                PropertyInfo pInfo = Master.GetType().GetProperty("LeftPanel");
                if (pInfo != null)
                {
                    Control LeftPanel = pInfo.GetValue(Master, null) as Control;
                    if (LeftPanel != null)
                    {
                        LeftPanel.Controls.Add(ucBulletins);
                    }
                }
            }
        }
    }
}
